<?php defined('SYSPATH') or die('No direct access allowed.');

class Controller_CPanel_Menu extends Controller_CPanel {

    public function action_index() {

        //Check forms

        $action = Arr::get($_POST, 'action', NULL);
        if ($action === NULL)
            $action = Arr::get($_GET, 'action', '');
        
        switch ($action) {
            case 'add_new':
                self::add_new();
                break;
            default:
                break;
        }
        
        if (isset($_POST['up'])) {
            self::up_menu();
        } elseif (isset($_POST['down'])) {
            self::down_menu();
        } elseif (isset($_POST['save'])) {
            self::save_menu();
        } elseif (isset($_POST['remove'])) {
            self::remove_menu();
        }

        //Render content

        $this->title = __('Menu');
        $view = View::factory('cpanel/content/menu');

        $settings = Kohana::$config->load('app');

        $view->languages = Arr::get($settings, 'languages', array());

        $this->content = $view->render();
    }
    
    private function add_new() {
        $parent = Arr::get($_POST, 'parent_id');
        $title = Arr::get($_POST, 'title');
        $uri = Arr::get($_POST, 'uri');
        
        $menu = ORM::factory('menu');
        $menu->parent_id = $parent;
        $menu->title = $title;
        $menu->uri = $uri;
        
        try {
            $menu->save();
        } catch (ORM_Validation_Exception $e) {
            $errors = $e->errors('validation');
            foreach ($errors as $error)
                parent::add_message($error, 'error');
            return;
        }
    }
    
    public function up_menu() {
        $menu = ORM::factory('menu', Arr::get($_POST, 'id', -1));
        if (!$menu->loaded()) {
            parent::add_message('Not found!!!', 'error');
            return;
        }
        
        $prev_menu = ORM::factory('menu')
                ->where('parent_id', '=', $menu->parent_id)
                ->where('pos', '=', $menu->pos - 1)
                ->find();
        
        if (!$prev_menu->loaded())
            return;
        
        $prev_menu->pos = $menu->pos;
        $menu->pos = $menu->pos - 1;
        
        $menu->save();
        $prev_menu->save();
    }
    
    public function down_menu() {
        $menu = ORM::factory('menu', Arr::get($_POST, 'id', -1));
        if (!$menu->loaded()) {
            parent::add_message('Not found!!!', 'error');
            return;
        }
        
        $next_menu = ORM::factory('menu')
                ->where('parent_id', '=', $menu->parent_id)
                ->where('pos', '=', $menu->pos + 1)
                ->find();
        
        if (!$next_menu->loaded())
            return;
        
        $next_menu->pos = $menu->pos;
        $menu->pos = $menu->pos + 1;
        
        $menu->save();
        $next_menu->save();
    }
    
    public function save_menu() {
        $menu = ORM::factory('menu', Arr::get($_POST, 'id', -1));
        if (!$menu->loaded()) {
            parent::add_message('Not found!!!', 'error');
            return;
        }
        
        $menu->title = Arr::get($_POST, 'title', '');
        $menu->uri = Arr::get($_POST, 'uri', '');
        
        try {
            $menu->save();
        } catch (ORM_Validation_Exception $e) {
            $errors = $e->errors('validation');
            foreach ($errors as $error)
                parent::add_message($error, 'error');
            return;
        }
        
        parent::add_message('Saved!!!', 'success');
    }
    
    public function remove_menu() {
        $menu = ORM::factory('menu', Arr::get($_POST, 'id', -1));
        if (!$menu->loaded()) {
            parent::add_message('Not found!!!', 'error');
            return;
        }
        
        $menu->delete();
        parent::add_message('Removed!!!', 'success');
    }

}